/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TransformingPanel.java
 *
 * Created on 2009-04-21, 11:46:27
 */
package panel;

import cytoscape.CyEdge;
import cytoscape.Cytoscape;
import cytoscape.data.CyAttributes;
import java.util.List;
import methods.DoubleTransformingMethod;
import methods.TransformingMethodExp;
import methods.TransformingMethodLog;
import methods.TransformingMethodMinus;
import methods.TransformingMethodPlus;

/**
 *
 * @author misiek
 */
public class DoubleTransformingPanel extends javax.swing.JPanel {

    /** Creates new form TransformingPanel */
    public DoubleTransformingPanel() {
        initComponents();
        refreshAttrList();
    }
    /*
    private void addAttribute(String attr) {
    CyAttributes edgesAttributes = Cytoscape.getEdgeAttributes();
    String[] names = edgesAttributes.getAttributeNames();
    String[] newnames = new String[names.length + 1];
    for (int i = 0; i < names.length; i++) {
    newnames[i] = String.valueOf(names);
    }
    newnames[names.length] = String.valueOf(attr);
    }
     */

    private boolean isGoodAttr(String outputAttrName) {
        if (outputAttrName == null) {
            return false;
        }
        if (outputAttrName.equals("")) {
            return false;
        }
        CyAttributes edgesAttributes = Cytoscape.getEdgeAttributes();
        for (String attrName : edgesAttributes.getAttributeNames()) {
            if (attrName.equals(outputAttrName)) {
                return false;
            }
        }

        return true;
    }

    private void refreshAttrList() {
        inputAttrCombo.removeAllItems();
        CyAttributes edgesAttributes = Cytoscape.getEdgeAttributes();
        for (String attrName : edgesAttributes.getAttributeNames()) {
            final byte cyType = edgesAttributes.getType(attrName);
            if (cyType == CyAttributes.TYPE_FLOATING) {
                inputAttrCombo.addItem(attrName);
            }
        }
    }

    private void tramsform(DoubleTransformingMethod method, String inputAttrName, String outputAttrName) {
        CyAttributes edgesAttributes = Cytoscape.getEdgeAttributes();
        @SuppressWarnings("unchecked")
        List<CyEdge> edges = Cytoscape.getCurrentNetwork().edgesList();
        for (CyEdge edge : edges) {
            String edgeID = edge.getIdentifier();
            Double probOrNull = edgesAttributes.getDoubleAttribute(edgeID, inputAttrName);

            if (probOrNull != null) {
                Double valOrNull = method.transform(probOrNull);
                if (valOrNull != null) {
                    edgesAttributes.setAttribute(edgeID, outputAttrName, valOrNull);
                }
            }
        }


    }

    private DoubleTransformingMethod transformingMethod() {
        DoubleTransformingMethod method = null;
        if (logMethodRadio.isSelected()) {
            method = new TransformingMethodLog();
        } else if (expMethodRadio.isSelected()) {
            method = new TransformingMethodExp();
        } else if (plusMethodRadio.isSelected()) {
            Double arg;
            try {
                String argStr = plusMethodArgField.getText();
                arg = Double.valueOf(argStr);
            } catch (NumberFormatException e) {
                arg = Double.valueOf(0);
            }
            method = new TransformingMethodPlus(arg);
        } else {
            method = new TransformingMethodMinus();
        }
        return method;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        methodRadioGroup = new javax.swing.ButtonGroup();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        refreshButton = new javax.swing.JButton();
        transformButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        logMethodRadio = new javax.swing.JRadioButton();
        expMethodRadio = new javax.swing.JRadioButton();
        plusMethodRadio = new javax.swing.JRadioButton();
        minusMethodRadio = new javax.swing.JRadioButton();
        plusMethodArgField = new javax.swing.JTextField();
        plusMethodArgLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        outputAttrLabel = new javax.swing.JLabel();
        inputAttrLabel = new javax.swing.JLabel();
        inputAttrCombo = new javax.swing.JComboBox();
        outputAttrField = new javax.swing.JTextField();

        jPanel4.setName("jPanel4"); // NOI18N

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Actions"));
        jPanel3.setName("jPanel3"); // NOI18N

        refreshButton.setText("Refresh");
        refreshButton.setName("refreshButton"); // NOI18N
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        transformButton.setText("Transform");
        transformButton.setName("transformButton"); // NOI18N
        transformButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                transformButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(refreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(transformButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(refreshButton)
                    .addComponent(transformButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Transforming method"));
        jPanel1.setName("jPanel1"); // NOI18N

        methodRadioGroup.add(logMethodRadio);
        logMethodRadio.setSelected(true);
        logMethodRadio.setText("log(x)");
        logMethodRadio.setName("logMethodRadio"); // NOI18N
        logMethodRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logMethodRadioActionPerformed(evt);
            }
        });

        methodRadioGroup.add(expMethodRadio);
        expMethodRadio.setText("e^x");
        expMethodRadio.setName("expMethodRadio"); // NOI18N
        expMethodRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                expMethodRadioActionPerformed(evt);
            }
        });

        methodRadioGroup.add(plusMethodRadio);
        plusMethodRadio.setText("x+a");
        plusMethodRadio.setName("plusMethodRadio"); // NOI18N
        plusMethodRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                plusMethodRadioActionPerformed(evt);
            }
        });

        methodRadioGroup.add(minusMethodRadio);
        minusMethodRadio.setText("-x");
        minusMethodRadio.setName("minusMethodRadio"); // NOI18N
        minusMethodRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minusMethodRadioActionPerformed(evt);
            }
        });

        plusMethodArgField.setText("0");
        plusMethodArgField.setEnabled(false);
        plusMethodArgField.setName("plusMethodArgField"); // NOI18N

        plusMethodArgLabel.setText("a:=");
        plusMethodArgLabel.setName("plusMethodArgLabel"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(logMethodRadio)
                    .addComponent(expMethodRadio)
                    .addComponent(minusMethodRadio)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(plusMethodRadio)
                        .addGap(18, 18, 18)
                        .addComponent(plusMethodArgLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(plusMethodArgField, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(logMethodRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(expMethodRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(minusMethodRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(plusMethodRadio)
                    .addComponent(plusMethodArgLabel)
                    .addComponent(plusMethodArgField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Attribute names settings"));
        jPanel2.setName("jPanel2"); // NOI18N

        outputAttrLabel.setText("Output double attr: ");
        outputAttrLabel.setName("outputAttrLabel"); // NOI18N

        inputAttrLabel.setText("Input double attr:");
        inputAttrLabel.setName("inputAttrLabel"); // NOI18N

        inputAttrCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        inputAttrCombo.setName("inputAttrCombo"); // NOI18N

        outputAttrField.setName("outputAttrField"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputAttrLabel)
                    .addComponent(outputAttrLabel))
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(inputAttrCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(outputAttrField, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputAttrLabel)
                    .addComponent(inputAttrCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputAttrLabel)
                    .addComponent(outputAttrField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void expMethodRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_expMethodRadioActionPerformed
        plusMethodArgField.setEnabled(false);
}//GEN-LAST:event_expMethodRadioActionPerformed

    private void plusMethodRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_plusMethodRadioActionPerformed
        plusMethodArgField.setEnabled(true);
    }//GEN-LAST:event_plusMethodRadioActionPerformed

    private void minusMethodRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minusMethodRadioActionPerformed
        plusMethodArgField.setEnabled(false);
    }//GEN-LAST:event_minusMethodRadioActionPerformed

    private void logMethodRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logMethodRadioActionPerformed
        plusMethodArgField.setEnabled(false);
    }//GEN-LAST:event_logMethodRadioActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        refreshAttrList();
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void transformButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_transformButtonActionPerformed
        DoubleTransformingMethod method = transformingMethod();

        String inputAttrName = (String) inputAttrCombo.getSelectedItem();
        String outputAttrName = outputAttrField.getText();
        if (isGoodAttr(outputAttrName)) {
            tramsform(method, inputAttrName, outputAttrName);
        } else {
            AttrNameExistDialog dial = new AttrNameExistDialog(Cytoscape.getDesktop(), true);
            dial.pack();
            dial.setVisible(true);
        }
    }//GEN-LAST:event_transformButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton expMethodRadio;
    private javax.swing.JComboBox inputAttrCombo;
    private javax.swing.JLabel inputAttrLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JRadioButton logMethodRadio;
    private javax.swing.ButtonGroup methodRadioGroup;
    private javax.swing.JRadioButton minusMethodRadio;
    private javax.swing.JTextField outputAttrField;
    private javax.swing.JLabel outputAttrLabel;
    private javax.swing.JTextField plusMethodArgField;
    private javax.swing.JLabel plusMethodArgLabel;
    private javax.swing.JRadioButton plusMethodRadio;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton transformButton;
    // End of variables declaration//GEN-END:variables
}
